package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.BizCollectorConfig;
import cn.stylefeng.guns.modular.gridsystem.entity.BizCollectorConfigTemplate;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizCollectorConfigTemplateParam;
import cn.stylefeng.guns.modular.gridsystem.model.param.CollectorConfigParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizCollectorConfigTemplateService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.gridsystem.service.BizCollectorConfigService;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizCollectorConfigParam;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 8路采集器配置 控制器
 *
 * @author: ssy
 * @date: 2022/02/18 13:58
 */
@RestController
public class BizCollectorConfigController {

    @Resource
    private BizCollectorConfigService bizCollectorConfigService;

    @Resource
    private BizCollectorConfigTemplateService bizCollectorConfigTemplateService;

    /**
     * 分页查询8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @GetMapping("/bizCollectorConfig/page")
    @BusinessLog(title = "8路采集器配置_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizCollectorConfigParam bizCollectorConfigParam) {
        return new SuccessResponseData(bizCollectorConfigService.page(bizCollectorConfigParam));
    }

    /**
     * 获取全部8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @GetMapping("/bizCollectorConfig/list")
    @BusinessLog(title = "8路采集器配置_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizCollectorConfigParam bizCollectorConfigParam) {
        return new SuccessResponseData(bizCollectorConfigService.list(bizCollectorConfigParam));
    }

    /**
     * 查看详情8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @GetMapping("/bizCollectorConfig/detail")
    @BusinessLog(title = "8路采集器配置_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizCollectorConfigParam.detail.class) BizCollectorConfigParam bizCollectorConfigParam) {
        return new SuccessResponseData(bizCollectorConfigService.detail(bizCollectorConfigParam));
    }

    /**
     * 添加8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @PostMapping("/bizCollectorConfig/add")
    @BusinessLog(title = "8路采集器配置_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizCollectorConfigParam.add.class) BizCollectorConfigParam bizCollectorConfigParam) {
        bizCollectorConfigService.add(bizCollectorConfigParam);
        return new SuccessResponseData();
    }

    @PostMapping("/bizCollectorConfig/addConfig")
    public ResponseData addConfig(@RequestBody CollectorConfigParam collectorConfigParam) {
        List<Map<String, Object>> codes = collectorConfigParam.getCodes();
        for (Map<String, Object> map : codes) {
            String code = map.get("code").toString();
            BizCollectorConfigTemplateParam bizCollectorConfigTemplateParam = new BizCollectorConfigTemplateParam();
            bizCollectorConfigTemplateParam.setGatewayTypeCode(code);
            List<BizCollectorConfigTemplate> list = bizCollectorConfigTemplateService.list(bizCollectorConfigTemplateParam);
            for (BizCollectorConfigTemplate bizCollectorConfigTemplate : list) {
                BizCollectorConfigParam bizCollectorConfigParam1 = new BizCollectorConfigParam();
                bizCollectorConfigParam1.setGatewayId(collectorConfigParam.getGatewayId());
                bizCollectorConfigParam1.setGatewayTypeCode(bizCollectorConfigTemplate.getGatewayTypeCode());
                bizCollectorConfigParam1.setCode(bizCollectorConfigTemplate.getCode());
                List<BizCollectorConfig> list1 = bizCollectorConfigService.list(bizCollectorConfigParam1);
                if (list1.size() > 0) {
                    continue;
                }
                BizCollectorConfigParam bizCollectorConfigParam = new BizCollectorConfigParam();
                bizCollectorConfigParam.setGatewayId(collectorConfigParam.getGatewayId());
                bizCollectorConfigParam.setGatewayTypeCode(bizCollectorConfigTemplate.getGatewayTypeCode());
                bizCollectorConfigParam.setCode(bizCollectorConfigTemplate.getCode());
                bizCollectorConfigParam.setValue(bizCollectorConfigTemplate.getValue());
                bizCollectorConfigParam.setRemark(bizCollectorConfigTemplate.getRemark());
                bizCollectorConfigParam.setConfigName(bizCollectorConfigTemplate.getConfigName());
                bizCollectorConfigParam.setStatus(bizCollectorConfigTemplate.getStatus());
                bizCollectorConfigService.add(bizCollectorConfigParam);
            }

        }
        return new SuccessResponseData();
    }

    /**
     * 删除8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @PostMapping("/bizCollectorConfig/delete")
    @BusinessLog(title = "8路采集器配置_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizCollectorConfigParam.delete.class) BizCollectorConfigParam bizCollectorConfigParam) {
        bizCollectorConfigService.delete(bizCollectorConfigParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑8路采集器配置
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @PostMapping("/bizCollectorConfig/edit")
    @BusinessLog(title = "8路采集器配置_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizCollectorConfigParam.edit.class) BizCollectorConfigParam bizCollectorConfigParam) {
        bizCollectorConfigService.edit(bizCollectorConfigParam);
        return new SuccessResponseData();
    }

    /**
     * 修改状态
     *
     * @author ssy
     * @date 2022/02/18 13:58
     */
    @PostMapping("/bizCollectorConfig/changeStatus")
    @BusinessLog(title = "8路采集器配置_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData changeStatus(@RequestBody @Validated(BizCollectorConfigParam.changeStatus.class) BizCollectorConfigParam bizCollectorConfigParam) {
        bizCollectorConfigService.changeStatus(bizCollectorConfigParam);
        return new SuccessResponseData();
    }

}
